<?php


namespace App\Listeners\Refund;


use App\Events\Refund;
use App\Services\PosApi\AliPayRefund;

class AliPay
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param Refund $event
     * @return void
     */
    public function handle(Refund $event)
    {
        $info = $event->info;

        if ($info['refund_amount'] > 0) {
            $sqbService = new AliPayRefund();
            foreach ($info['items'] as $item) {
                if ($item['pay_platform'] == $sqbService->paymentCode) {
                    $money = min($info['refund_amount'],$item['pay_amount']);
                    $result = $sqbService->handle($info,$item,$money);
                    $event->info['refund_amount'] -= $money;
                }
            }
        } else {
            return false;
        }
    }
}